import { format } from 'date-fns';

export function getDateString(date, type) {
    let lastTime = new Date(date);
    let currentTime = new Date();
    let timeDiff = currentTime - lastTime;
    let daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

    if (type == 0) {
        if (daysDiff === 0) {
            return format(lastTime, 'HH:mm');
        } else if (daysDiff === 1) {
            return '昨天 ' + format(lastTime, 'HH:mm');
        } else if (daysDiff === 2) {
            return '前天 ' + format(lastTime, 'HH:mm');
        } else if (lastTime.getFullYear() === currentTime.getFullYear()) {
            return format(lastTime, 'MM/dd');
        } else {
            return format(lastTime, 'yyyy/MM/dd');
        }
    } else if (type == 1) {
        if (daysDiff === 0) {
            return format(lastTime, 'HH:mm');
        } else if (daysDiff === 1) {
            return '昨天 ' + format(lastTime, 'HH:mm');
        } else if (daysDiff === 2) {
            return '前天 ' + format(lastTime, 'HH:mm');
        } else if (lastTime.getFullYear() === currentTime.getFullYear()) {
            return format(lastTime, 'MM/dd HH:mm');
        } else {
            return format(lastTime, 'yyyy/MM/dd HH:mm');
        }
    }
}

export function deepGet(data) {
    return JSON.parse(JSON.stringify(data))
}

export function formatDate(isoDate) {
    const date = new Date(isoDate);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}

export function formatDateToSecond(isoDate) {
    const date = new Date(isoDate);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

export function getFormatDate(isoDate) {
    const date = new Date(isoDate);
    if (isNaN(date.getTime())) {
        return '无效日期';
    }
    return format(date, 'yyyy年MM月dd日 HH:mm');
}